﻿using System.Collections.Generic;
using System;
using System.Linq;

namespace PluginDB
{
    /// <summary>
    /// Summary description for PluginVideoInfo
    /// </summary>
    public class PluginVideoInfo
    {
        #region Properties
        public long VideoID { get; set; }
        public string HebrewVideoName { get; set; }
        public string EnglishVideoName { get; set; }
        public string PluginUrl { get; set; }
        public string SiteName { get; set; }
        public string Plot { get; set; }
        public string ImageUrl { get; set; }
        public List<Link> Links { get; set; }
        public bool IsHebDub { get; set; }
        public bool IsHebSub { get; set; }
        public bool IsTVShow { get; set; }
        public TVShowInfo TVShowInformation { get; set; }
        public List<string> Categories { get; set; }

        private string _videoName;
        public string VideoName
        {
            get
            {
                return _videoName;
            }
            set
            {
                this._videoName = value;
                this.ExtractVideoInfo();
            }
        } 
        #endregion

        #region Public Class Members
        public class TVShowInfo
        {
            public string SeriesName { get; set; }
            public int SeasonNumber { get; set; }
            public TVDBInformationItem TVDBInformationItem { get; set; }
            public int EpisodeNumber { get; set; }
        }

        public class Link
        {
            public string LinkURL { get; set; }
            public string SourceName { get; set; }
            public bool IsBrokenLink { get; set; }
        } 
        #endregion

        #region Ctor
        public PluginVideoInfo()
        {
            this.Links = new List<Link>();
            this.Categories = new List<string>();
            this.TVShowInformation = new TVShowInfo();
        } 
        #endregion

        #region Public Methods
        public void ExtractTVShowInfo(
           out string seriesName,
           out int seasonNumber,
           out int episodeNumber)
        {
            var splitHebrewVideoName = AlignStringToRTL(this.HebrewVideoName);

            var seriesNameList = new List<string>();
            seasonNumber = 0;
            episodeNumber = 0;

            for (var i = 0; i < splitHebrewVideoName.Length; i++)
            {
                if (splitHebrewVideoName[i].Trim().Equals("עונה"))
                {
                    if (char.IsDigit(splitHebrewVideoName[i + 1], 0))
                    {
                        i++;
                        seasonNumber = ExtractNumber(splitHebrewVideoName[i]);
                    }
                    else
                    {
                        seasonNumber = 1;
                    }
                    continue;
                }

                if (splitHebrewVideoName[i].Trim().Equals("פרק"))
                {
                    if (char.IsDigit(splitHebrewVideoName[i + 1], 0))
                    {
                        //If the episode string has more text after it and then the number we want to find it
                        if (!char.IsDigit(splitHebrewVideoName[i], 0) &&
                            (i + 1 < splitHebrewVideoName.Length &&
                            char.IsDigit(splitHebrewVideoName[i + 1], 0)))
                        {
                            i++;
                            episodeNumber = ExtractNumber(splitHebrewVideoName[i]);
                        }
                    }
                    continue;
                }

                if (splitHebrewVideoName[i].Trim().Equals("מיני סדרה"))
                {
                    seasonNumber = 1;
                }

                if (splitHebrewVideoName[i].Trim().Equals("פרקים"))
                {
                    if (char.IsDigit(splitHebrewVideoName[i + 1], 0))
                    {
                        i++;
                        episodeNumber = ExtractNumber(splitHebrewVideoName[i]);
                    }
                    continue;
                }

                seriesNameList.Add(splitHebrewVideoName[i].Trim());
            }
            seriesName = string.Join(" ", seriesNameList.ToArray());
        } 
        #endregion

        #region Private Methods
        private void ExtractVideoInfo()
        {
            this.CleanMovieName();

            this.HebrewVideoName = this._videoName;
            this.EnglishVideoName = this._videoName;

            //Clean video name in order to be sure what we parse
            this._videoName = this._videoName.Replace("-", " ").Replace(",", " ");

            if (this._videoName.Contains("/") || this._videoName.Contains("\\"))
            {
                var splited = this._videoName.Split(new[] { "/", "\\" }, StringSplitOptions.RemoveEmptyEntries);
                if (IsContainsHebrew(splited[0]))
                {
                    this.HebrewVideoName = splited[0].Trim();
                    this.EnglishVideoName = splited[1].Trim();
                }
                else
                {
                    this.HebrewVideoName = splited[1].Trim();
                    this.EnglishVideoName = splited[0].Trim();
                }
            }
        }

        private string[] AlignStringToRTL(string text)
        {
            var splitName = text.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            if (char.IsDigit(splitName[0], 0) && splitName.Length > 1)
            {
                if (!char.IsDigit(splitName[splitName.Length - 1], 0) &&
                    (splitName[splitName.Length - 1].Equals("פרק") ||
                    splitName[splitName.Length - 1].Equals("פרקים")))
                {
                    var newText = new List<string>();
                    for (var index = 1; index < splitName.Length; ++index)
                    {
                        newText.Add(splitName[index]);
                    }
                    newText.Add(splitName[0]);
                    return newText.ToArray();
                }
            }
            return splitName;
        }

        private int ExtractNumber(string text)
        {
            int extractedNumber = 0;
            var splitedText = text.Trim().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            if (splitedText.Length > 0)
            {
                int.TryParse(splitedText[0], out extractedNumber);
            }
            return extractedNumber;
        }

        private bool IsContainsHebrew(IEnumerable<char> text)
        {
            const int hebrewMinValue = 0x0590;
            const int hebrewMaxValue = 0x05FF;

            return text.Any(myChar => (myChar >= hebrewMinValue) && (myChar <= hebrewMaxValue));
        }

        private void CleanMovieName()
        {
            this._videoName = this._videoName.Replace("צפייה ישירה", string.Empty);
            this._videoName = this._videoName.Replace("צפיה ישירה", string.Empty);
            this._videoName = this._videoName.Replace("&#039;", string.Empty);
            this._videoName = this._videoName.Replace("[", string.Empty);
            this._videoName = this._videoName.Replace("]", string.Empty);
            this._videoName = this._videoName.Replace("*", string.Empty);
            this._videoName = this._videoName.Replace(",", string.Empty);
        } 
        #endregion
    }
}